--	/**
--	 * 
--	 * 直线运动 接近真实的匀速，移动更平滑，无抖动,受帧率的影响小
--	 * 
--	 * 适用于玩家自己的移动控制
--	 * @author yangxuefeng
--	 * 
--	 */
local Array =import("engine.struct.Array")
local MoveToAnimation =class("MoveToAnimation") 

--		/**
--		 * 
--		 * @param obj 要移动的对象 如英雄
--		 * @param speed 移动速度 每一秒移动的像素
--		 * @param endX  移动目标点
--		 * @param endY  移动目标点
--		 * @param bg    对象的背景层
--		 * @param container 可显示区域的容器对象，如castle
--		 * 
--		 */		
		 function MoveToAnimation:ctor(obj,speed,endX,endY)
			self._obj =obj; 
			self._speed = speed;
			self._endX = endX;
			self._endY = endY;
		    self._callBackMoving =Array.new();
		    self._callBackCompleted =Array.new();
		    self._callBackPaused = Array.new();
		    self._callBackStart = Array.new();
		    
		 end	
		
		 function MoveToAnimation:empty () 
			self._obj = nil ;
			self._callBackMoving = nil ;
			self._callBackCompleted = nil ;
			self._callBackPaused = nil ;
			self._callBackStart = nil ;
		 end
--		/**
--		 *重新设置移动目标点，并停止之前的动画。这样避免频繁创建对象 
--		 * @param endX
--		 * @param endY
--		 * 
--		 */
		 function MoveToAnimation:resetTo(endX,endY)			
			self._endX = endX;
			self._endY = endY;	
		end		
		
		
--		/**
--		 *用callback的方式 替代事件机制，可以获得更好的性能 
--		 * @param type
--		 * @param listener
--		 * 
--		 */
		  function MoveToAnimation:addEventListener(type, listener)
		    
				if type =="MOVING" then				  
					self._callBackMoving:push(listener);
				elseif type == "ONSTART" then
					self._callBackStart:push(listener);
				elseif type== "COMPLETED" then
					self._callBackCompleted:push(listener);
				elseif type =="PAUSED" then
					self._callBackPaused:push(listener);
				end
				
					
			end
			
		
		 function MoveToAnimation:removeEventListener(type, _callBack)
			   if type =="MOVING" then
			      self._callBackMoving:removeObj(_callBack)
			   elseif type=="ONSTART" then
			     self._callBackStart:removeObj(_callBack)
			   elseif type == "COMPLETED" then
			     self._callBackCompleted:removeObj(_callBack)
			   elseif type =="PAUSED" then
			     self._callBackPaused:removeObj(_callBack)
			   end
		end
			
			
		

			
--		
--		/**
--		 *开始移动 
--		 * 
--		 */
		  function MoveToAnimation:start(now)	
		 
		    
			self._isFinish =false;
			self._isMoving =true;
			self:resume();
			local objX =self._obj:getPositionX()
			local objY =self._obj:getPositionY()
			self._nx=objX;
			self._ny= objY;			
      self._startX =objX
      self._startY =objY
			local dxSum =self._endX-self._startX;
			local dySum =self._endY-self._startY;
			
			self._t1=now
			self._t2=0;
			local s =math.sqrt(dxSum*dxSum+dySum*dySum);
			self._leftTime =1000*(s/self._speed);		
			self._deltaX =dxSum/self._leftTime;
			self._deltaY =dySum/self._leftTime;
			
	     end
	
		
		
		 function MoveToAnimation:doRender(now)
		  			
			if self._isPaused or self._isFinish  then return end;
			local currentTime =now
			self._t2 =currentTime -self._t1;--一次计时周期所消耗的时间
			self._t1=currentTime;			
			if self._leftTime<0 then
				self:stop();
				return;
			end
			
			if self._leftTime<=self._t2 then--剩下的时间不到一个周期，直接完成
				self._leftTime =-1;
				self:setXY(self._endX,self._endY)
				self:stop();
			
			else
			
				self._nx = self._nx+self._deltaX*self._t2;--小数点不能去掉
				self._ny = self._ny+self._deltaY*self._t2;
				self:setXY(toint(self._nx),toint(self._ny))
				self._leftTime=self._leftTime-self._t2;	
				self:dispatchEvent("MOVING");
			
			end		
	end
		
		function MoveToAnimation:setXY(x,y)
          self._obj:setPositionX(x);
          self._obj:setPositionY(y);
		end
		
		  
     function MoveToAnimation:dispatchEvent(event)
      local obj =self._obj
      if event == "MOVING" then
         self._callBackMoving:foreach(function(callback) callback(obj) end)
      elseif event == "ONSTART" then
         self._callBackStart:foreach(function(callback) callback(obj) end)
      elseif event == "COMPLETED" then
         self._callBackCompleted:foreach(function(callback) callback(obj) end)
      elseif event == "PAUSED" then
         self._callBackPaused:foreach(function(callback) callback(obj) end)
      end
    end
      
      
	
		
--		/**
--		 *完成，会触发完成事件，在一个移动没有真正完成前，请使用pause来中止。 
--		 * 
--		 */
		 function MoveToAnimation:stop()
			self._isMoving = false;
			self._isFinish = true;	
			self:dispatchEvent("COMPLETED");		
		end
		
			
		 
		
		 function MoveToAnimation:resume()
			self._isPaused = false;
		 end
		
		 function MoveToAnimation:pause()
			self._isPaused = true;			
			self:dispatchEvent("PAUSED");
		end
		

return MoveToAnimation
	
	
